package com.sqx.modules.health.utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Dom4jParser
 * Dom4j读写xml文件
 * @author
 * @date 2022/1/8
 */
public class Dom4jParser {

    public static String xmlFilePath = "C:\\Patient2.xml";
    public static Element rootElement = null;
    /**
     * web环境读取xml
     * @param request
     * @return
     * @throws Exception
     */
    public static Map<String, String> parseXml(HttpServletRequest request)
            throws Exception {
        // 解析结果存储在HashMap
        Map<String, String> map = new HashMap<String, String>();
        InputStream inputStream = request.getInputStream();
        // 读取输入流
        SAXReader reader = new SAXReader();
        Document document = reader.read(inputStream);
        // 得到xml根元素
        Element root = document.getRootElement();
        // 得到根元素的所有子节点
        List<Element> elementList = root.elements();

        // 遍历所有子节点
        for (Element e : elementList)
            map.put(e.getName(), e.getText());

        // 释放资源
        inputStream.close();
        inputStream = null;
        return map;
    }

    /**
     * cmd环境读取xml
     * @param content
     * @return
     * @throws Exception
     */
    public static Map<String, String> parseXml(String content)
            throws Exception {
        // 解析结果存储在HashMap
        Map<String, String> map = new HashMap<String, String>();
        SAXReader reader = new SAXReader();
        Document document = reader.read(new ByteArrayInputStream(content.getBytes()));
        // 得到xml根元素
        Element root = document.getRootElement();
        // 得到根元素的所有子节点
        List<Element> elementList = root.elements();

        // 遍历所有子节点
        for (Element e : elementList)
            map.put(e.getName(), e.getText());
        return map;
    }

    /**
     * 写入xml
     * @return
     */
    public static String writeXml(){
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("xml");
        Element appid = root.addElement("appid");
        appid.addCDATA("appidvalue");
        return root.asXML();
    }
    public static String patInfo(Map<String, String> map){
        rootElement.element("PatName").setText(map.get("PatName"));
        rootElement.element("PatAge").setText(map.get("PatAge"));
        rootElement.element("PatHeight").setText(map.get("PatHeight"));
        rootElement.element("PatName").setText(map.get("PatName"));
        rootElement.element("PatName").setText(map.get("PatName"));
        rootElement.element("PatName").setText(map.get("PatName"));
        return rootElement.asXML();
    }
    public void downloadLocal(String fileUUID, HttpServletResponse response) throws FileNotFoundException {

        Map<String, String> map = new HashMap<String, String>();
        Dom4jParser.patInfo(map);
        // 下载本地文件
        String fileName = xmlFilePath; // 文件的默认保存名
        // 根据文件的唯一标识码获取文件
//        File uploadFile = new File(fileUploadPath + "/" + fileUUID);
        // 读到流中
        InputStream inStream = new FileInputStream(fileName);// 文件的存放路径
        // 设置输出的格式
        response.reset();
        response.setContentType("text/plain;charset=UTF-8");
        response.addHeader("Content-Disposition", "attachment; filename=\"" + fileUUID);
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = inStream.read(b)) > 0)
                response.getOutputStream().write(b, 0, len);
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取xml
     * @return
     */
    public static Element readXml(){
        SAXReader saxReader = new SAXReader();
        Document document = null;
        try {
            document = saxReader.read(new File(xmlFilePath));
            rootElement = document.getRootElement();  // 获得根节点(tysoft)
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return rootElement;
    }

    public static void main(String[] args) throws Exception {
        String content="<xml><appid>gegege</appid><attach><![CDATA[GEGEGE]]></attach></xml>";
//        读取xml
        readXml();
       // parseXml(content);
//        写入xml
        Map<String, String> map = new HashMap<String, String>();
        map.put("KKT.Pationt.INFO","");
        map.put("Pat_ID","");
        map.put("PatName","王小明");
        map.put("PatAge","28");
        map.put("PatBirthday","");
        map.put("PatSex","男");
        map.put("PatHeight","180");
        map.put("PatWeight","160");
        map.put("PatResidentID","");
        map.put("PatEmail","");
        map.put("PatTel","");
        map.put("PatAddr","");
        map.put("Diagnose","");
        map.put("Conclusion","");
        map.put("Record_ID","");
        map.put("AdmissionID","");
        map.put("CLinicID","");
        map.put("BedID","");
        map.put("Department","");
        map.put("Doctor","赵胜");
        map.put("DiagnoseDoctor","赵胜2");
        map.put("Lable","");
        map.put("DepartmentType","");
        map.put("Verified","");
        map.put("PaceType","");
        map.put("HWDataType","");
        map.put("Status","");
        map.put("ECGChannelCount","12");
        map.put("CollectNetName","");
        map.put("AnalysisNetName","");
        map.put("SendDate","");
        map.put("ReceiveDate","");
        map.put("PrintDate","");
        map.put("RecordDate","2019-03-12 17:06:20");
        map.put("PlayBackDate","");
        map.put("DBRecordID","KKT.ECG");
        map.put("TotalTime","");
        map.put("HospitalName","南阳市第八人民医院");
        map.put("LoginName","1");
        map.put("LoginSignatureFile","Y:\\Desktop\\工程接口\\holter软件分析模块\\holter-out\\Signature1.png");
        map.put("SignatureFile","Y:\\Desktop\\工程接口\\holter软件分析模块\\holter-out\\Signature1.png");
        map.put("ReportSignatureFile","");
        map.put("Permission","1");



String path="";
        path=  patInfo(map);
        System.out.println(path);
    }
}
